<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="spel,">










<meta name="description" content="spel总结背景：在Spring Boot 中经常通过#{}或者${}这两种方式挂到属性上面进行获取内容，但是又经常获取不到，现在来仔细了解一下这两个的区别。 Spel表达式例子​    spel 表达式(Spring Expression Language),这种方式是通过#{…},进行获取，里面是spel表达式 下面通过一些例子进行了解下 通过Spring Boot 进行启动 12345678">
<meta name="keywords" content="spel">
<meta property="og:type" content="article">
<meta property="og:title" content="spel表达式">
<meta property="og:url" content="http://yoursite.com/2019/09/01/spel表达式/index.html">
<meta property="og:site_name" content="likuoBlog">
<meta property="og:description" content="spel总结背景：在Spring Boot 中经常通过#{}或者${}这两种方式挂到属性上面进行获取内容，但是又经常获取不到，现在来仔细了解一下这两个的区别。 Spel表达式例子​    spel 表达式(Spring Expression Language),这种方式是通过#{…},进行获取，里面是spel表达式 下面通过一些例子进行了解下 通过Spring Boot 进行启动 12345678">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-09-01T09:46:27.141Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="spel表达式">
<meta name="twitter:description" content="spel总结背景：在Spring Boot 中经常通过#{}或者${}这两种方式挂到属性上面进行获取内容，但是又经常获取不到，现在来仔细了解一下这两个的区别。 Spel表达式例子​    spel 表达式(Spring Expression Language),这种方式是通过#{…},进行获取，里面是spel表达式 下面通过一些例子进行了解下 通过Spring Boot 进行启动 12345678">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/09/01/spel表达式/">





  <title>spel表达式 | likuoBlog</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?cade855f62b1149c67ba58f84c9664f4";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">likuoBlog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/09/01/spel表达式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="likuo">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="likuoBlog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">spel表达式</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-01T17:41:17+08:00">
                2019-09-01
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="spel总结"><a href="#spel总结" class="headerlink" title="spel总结"></a>spel总结</h1><p>背景：在Spring Boot 中经常通过<code>#{}</code>或者<code>${}</code>这两种方式挂到属性上面进行获取内容，但是又经常获取不到，现在来仔细了解一下这两个的区别。</p>
<h2 id="Spel表达式"><a href="#Spel表达式" class="headerlink" title="Spel表达式"></a>Spel表达式</h2><h3 id="例子"><a href="#例子" class="headerlink" title="例子"></a>例子</h3><p>​    spel 表达式(Spring Expression Language),这种方式是通过#{…},进行获取，里面是spel表达式</p>
<p>下面通过一些例子进行了解下</p>
<p>通过Spring Boot 进行启动</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span>(value = <span class="string">"controllerTest"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RestControllerTest</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Log log = LogFactory.getLog(RestControllerTest.class);</span><br><span class="line">    <span class="meta">@Bean</span>(value = <span class="string">"userInfo"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> UserInfo <span class="title">getUserInfo</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> UserInfo(<span class="number">1l</span>,<span class="string">"spring 爸爸"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"#&#123;userInfo.name&#125;"</span>)</span><br><span class="line">    <span class="keyword">public</span> String father;</span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"#&#123;'my name is spring '&#125;"</span>)</span><br><span class="line">    <span class="keyword">public</span> String name;</span><br><span class="line">    <span class="comment">// 此处创建对象要写上包名+类名，否则会报错</span></span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"#&#123;new java.util.Date()&#125;"</span>)</span><br><span class="line">    <span class="keyword">public</span> Date date;</span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"#&#123;20&#125;"</span>)</span><br><span class="line">    <span class="keyword">public</span> Integer age;</span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"#&#123;1&#125;"</span>)</span><br><span class="line">    <span class="keyword">public</span> Integer sexValue;</span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"#&#123;controllerTest.sexValue==1?'男':'女'&#125;"</span>)</span><br><span class="line">    <span class="keyword">public</span> String sex;</span><br><span class="line">    <span class="comment">//  controllerTest.age 这里代表 当前bean的名称加上属性就可以直接获取</span></span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"#&#123;controllerTest.age&gt;18?'成年':'未成年'&#125;"</span>)</span><br><span class="line">    <span class="keyword">public</span> String remark;</span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"#&#123;controllerTest.age&gt;18 &amp;&amp; controllerTest.sexValue==1?'可以':'不可以'&#125;"</span>)</span><br><span class="line">    <span class="keyword">public</span> String goKTV;</span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"#&#123;1 lt 2&#125;"</span>)</span><br><span class="line">    <span class="keyword">public</span> Boolean a;</span><br><span class="line">    <span class="meta">@GetMapping</span>(<span class="string">"/start"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">aa</span><span class="params">()</span></span>&#123;</span><br><span class="line">        log.info(name);</span><br><span class="line">        log.info(<span class="string">"我的父亲是"</span>+father);</span><br><span class="line">        log.info(<span class="string">"当前时间是"</span>+date);</span><br><span class="line">        log.info(<span class="string">"我今年"</span>+age+<span class="string">"岁"</span>);</span><br><span class="line">        log.info(<span class="string">"我今年"</span>+remark);</span><br><span class="line">        log.info(<span class="string">"我"</span>+goKTV+<span class="string">"KTV唱歌"</span>);</span><br><span class="line">        log.info(a);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"hello spring boot"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserInfo</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Long id;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Long <span class="title">getId</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(Long id)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.id = id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">UserInfo</span><span class="params">(Long id, String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.id = id;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出内容：</p>
<blockquote>
<p>my name is spring<br>我的父亲是spring 爸爸<br>当前时间是Sun Sep 01 17:43:29 CST 2019<br>我今年20岁<br>我今年成年<br>我可以KTV唱歌<br>true</p>
</blockquote>
<table>
<thead>
<tr>
<th>运算符类型</th>
<th>运算符</th>
<th>例子</th>
</tr>
</thead>
<tbody><tr>
<td>算术运算</td>
<td>+、-、*、/、%、^</td>
<td>1+1、2^3、</td>
</tr>
<tr>
<td>比较运算</td>
<td>&lt;、&gt;、==、&lt;=、&gt;= 、lt、 gt、 eq 、le 、ge</td>
<td>1 lt 2</td>
</tr>
<tr>
<td>条件运算</td>
<td>?:(ternary) 、?:(Elvis)</td>
<td>三元运算符 A?true : false</td>
</tr>
<tr>
<td>逻辑运算</td>
<td>and、or、not、|</td>
<td>A and B</td>
</tr>
<tr>
<td>正则表达式</td>
<td>matches</td>
<td>#{email matches ‘[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.com’}</td>
</tr>
<tr>
<td>类型判断</td>
<td>instanceof</td>
<td>‘xyz’ instanceof T(int)</td>
</tr>
</tbody></table>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/spel/" rel="tag"># spel</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/09/01/Lambda-表达式-2/" rel="next" title="Lambda 表达式(2)">
                <i class="fa fa-chevron-left"></i> Lambda 表达式(2)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        
<script>
  with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='//bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>

      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC80NjIyMS8yMjczMg=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">likuo</p>
              <p class="site-description motion-element" itemprop="description">likuo</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#spel总结"><span class="nav-number">1.</span> <span class="nav-text">spel总结</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Spel表达式"><span class="nav-number">1.1.</span> <span class="nav-text">Spel表达式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#例子"><span class="nav-number">1.1.1.</span> <span class="nav-text">例子</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">likuo</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  

  

  

  

</body>
</html>
